package com.changil.admin.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.changil.admin.action.Action;
import com.changil.admin.action.user.LogOutAction;
import com.changil.admin.action.user.LoginAction;
import com.changil.admin.action.user.LoginFormAction;
import com.changil.admin.protocol.http.HttpUtil;

public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");

		String a = request.getParameter("a");
		if (a == null) {
			a = "";
		}

		Action action = null;
		if (a.equals("loginform")) {
			action = new LoginFormAction();
		} else if (a.equals("login")) {
			action = new LoginAction();
		}
		else if (a.equals("logout")) {
			action = new LogOutAction();
		}
		else {
			HttpUtil.redirect(response, "/NowonChangilChurch_Admin/main");
		}

		if (action != null) {
			action.execute(request, response);
		}
	}

}
